/* eslint-disable @typescript-eslint/no-parameter-properties */
/* eslint-disable no-useless-constructor */
import { toUpper } from "lodash";
import {
  IMatching,
  IParseResult,
  ITableInfo,
  ICompletionItem,
  IStatement,
  ICursorInfo,
  IParserType,
  ITableParams,
} from "./types";
import { SUGGEST_VERSION } from ".";

export class DefaultOpts {
  public parserType: IParserType = "mysql";

  public version: SUGGEST_VERSION = SUGGEST_VERSION.V1;

  private charsetList: string[] = [
    "utf8",
    "utf8mb4",
    "latin1",
    "gb2312",
    "big5",
  ];

  public language = "sql";

  constructor(private monaco: any) {
    // todo
  }

  public onParse = (_parseResult: IParseResult) => {
    //
  };
  // 环境
  public onSuggestEnvs?: () => Promise<ICompletionItem[]> = () => {
    return Promise.resolve([]);
  };
  // charset字符集列表
  public onSuggestCharsets?: () => Promise<ICompletionItem[]> = () => {
    return Promise.resolve(
      this.charsetList.map((name) => {
        return {
          label: name,
          insertText: name,
          sortText: `C_${name}`,
          kind: this.monaco.languages.CompletionItemKind.Folder,
        };
      })
    );
  };
  // 时间函数
  public onSuggestTimeFunction?: () => Promise<ICompletionItem[]> = () => {
    return Promise.resolve([]);
  };
  // UDF名称提示
  public onSuggestUdfName?: (
    cursorInfo: ICursorInfo<ITableInfo>
  ) => Promise<ICompletionItem[]> = () => {
    return Promise.resolve([]);
  };

  public onSuggestTableNames?: (
    cursorInfo?: ICursorInfo<ITableParams>
  ) => Promise<ICompletionItem[]> = (_cursorInfo) => {
    return Promise.resolve([]);
  };

  public onSuggestTableFields?: (
    tableInfo?: ITableInfo,
    cursorValue?: string,
    rootStatement?: IStatement
  ) => Promise<ICompletionItem[]> = (
    _tableInfo,
    _cursorValue,
    _rootStatement
  ) => {
    return Promise.resolve([]);
  };

  public pipeKeywords = (keywords: IMatching[]) => {
    return keywords
      .filter((matching) => {
        return matching.type === "string";
      })
      .map((matching) => {
        const value = /[a-zA-Z-]+/.test(matching.value.toString())
          ? toUpper(matching.value.toString())
          : matching.value.toString();
        return {
          label: value,
          insertText: value,
          documentation: "documentation",
          detail: "detail",
          kind: this.monaco.languages.CompletionItemKind.Keyword,
          sortText: `W_${matching.value}`,
        };
      });
  };

  public onSuggestFunctionName?: (
    inputValue?: string
  ) => Promise<ICompletionItem[]> = (_inputValue) => {
    return Promise.resolve(
      [].map((each) => {
        return {
          label: each,
          insertText: each,
          sortText: `C_${each}`,
          kind: this.monaco.languages.CompletionItemKind.Function,
        };
      })
    );
  };
  public onSuggestFieldGroup?: (tableNameOrAlias?: string) => ICompletionItem =
    (tableNameOrAlias) => {
      return {
        label: tableNameOrAlias as string,
        insertText: tableNameOrAlias,
        sortText: `D_${tableNameOrAlias}`,
        kind: this.monaco.languages.CompletionItemKind.Folder,
      };
    };

  public onHoverTableField?: (
    fieldName?: string,
    extra?: ICompletionItem
  ) => Promise<any> = (...args) => {
    return Promise.resolve([
      { value: "onHoverTableField" },
      {
        value: `\`\`\`json\n${JSON.stringify(args, null, 2)}\n\`\`\``,
      },
    ]);
  };

  public onHoverTableName?: (cursorInfo?: ICursorInfo) => Promise<any> = (
    ...args
  ) => {
    return Promise.resolve([
      { value: "onHoverTableName" },
      {
        value: `\`\`\`json\n${JSON.stringify(args, null, 2)}\n\`\`\``,
      },
    ]);
  };

  public onHoverFunctionName?: (functionName?: string) => Promise<any> = (
    ...args
  ) => {
    return Promise.resolve([
      { value: "onHoverFunctionName" },
      {
        value: `\`\`\`json\n${JSON.stringify(args, null, 2)}\n\`\`\``,
      },
    ]);
  };
}
